package bspay

import (
	"context"
	"fmt"

	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/pay/base"
	"gitee.com/liuxuezhan/ar-platform/pay-channel-packet/bspay/bspay"
)

type Factory struct {
}

func (p *Factory) CreatePay(config base.Config) base.Payment {
	return &Bspay{
		Url:        config.Url,
		MerchantId: config.MerchantId,
		SecretKey:  config.SecretKey,
		NotifyUrl:  config.NotifyUrl,
		ReturnUrl:  config.ReturnUrl,
		AppId:      config.AppId,
	}
}

type Bspay struct {
	MerchantId string
	NotifyUrl  string
	SecretKey  string
	Url        string
	AppId      string
	ReturnUrl  string
}

type Resp struct {
	Code      string `json:"code"`
	Timestamp int64  `json:"timestamp"`
	Message   string `json:"message"`
	Params    data   `json:"params"`
}
type data struct {
	MerchantRef  string `json:"merchant_ref"`
	SystemRef    string `json:"system_ref"`
	Amount       string `json:"amount"`
	Fee          string `json:"fee"`
	Status       int    `json:"status"`
	SuccessTime  int    `json:"success_time"`
	Payurl       string `json:"payurl"`
	ExtendParams string `json:"extend_params"`
	Product      string `json:"product"`
}

// 代收订单创建
func (bs *Bspay) Pay(ctx context.Context, req map[string]any) (resp base.PayResponse, err error) {
	appKey := bs.AppId
	amount := fmt.Sprint(req["amount"])
	orderID := req["orderNo"].(string)
	url := bs.Url + "/api/recharge"
	payResult, err := bspay.BsPay(url, appKey, orderID, bs.NotifyUrl, bs.ReturnUrl, bs.SecretKey, amount)
	if err != nil {
		return resp, err
	}
	resp = base.PayResponse{
		Url:     payResult.Url,
		OrderNo: payResult.OrdId,
	}
	return resp, nil

}
